package edu.bw.service.impl;

import edu.bw.mapper.ProductMapper;
import edu.bw.po.Product;
import edu.bw.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service // 注解为service层组件
public class ProductServiceImpl implements ProductService {
    @Autowired //在配置文件中配置了mapper的扫描路径就放到了Spring容器中管理 因此可以直接注入
    private ProductMapper productMapper;

//    @Transactional(readOnly = true) 查询一般不需要加事务 如果加就像前面那样开启只读
    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Transactional // 默认是readOnly=false
    @Override
    public void update(Product product) {
//        int i = 1 / 0;
        productMapper.update(product);
    }

    @Transactional
    @Override
    public void deleteByPid(Integer pid) {
        productMapper.deleteByPid(pid);
    }

    @Transactional
    @Override
    public void saveProduct(Product product) {
        productMapper.saveProduct(product);
    }
}
